<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/27
 * Time: 16:20
 */

namespace Task\App\Doorctrlkydx;

use Task\Core\RedisService;
use Task\Core\Base;

class Doorctrl extends Base
{
    public $mysql;

    public function __construct()
    {
        parent::__construct();
        $this->mysql = $this->selectDB('mysql');

    }
    public function kydx_door_log(){

//        把redis里缓存数据保存到当月记录数据库
        $date = date('Y_m');
        $table = 'kydx_door_log'.$date;

        $redis = RedisService::connectRedis();
        $cachekey ='kydx_mj_receiveKydxMj';
        $i=0;

        while ($r_data = json_decode($redis->rPop($cachekey),true)) {
            $i++;
            if($i%1000==0){
                sleep(1);
            }
            $data['door_id'] =$r_data['door_id'];
            $data['create_time'] = $r_data['create_time'];
            $data['device_time'] = $r_data['device_time'];
            $data['result_code'] = $r_data['result_code'];
            $data['open_type'] = $r_data['open_type'];
            $result = $this->mysql->insert($table,$data);

            echo $result;
        }
    }
        //根据时间日期生成表
    public function kydx_creation_log_table(){

        $date = date('Y_m',strtotime('+1 month'));
        $table = 'kydx_door_log'.$date;

        $sql = "
            CREATE TABLE If Not Exists `gc_{$table}` (
              `log_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
              `door_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '门ID',
              `create_time` datetime NOT NULL COMMENT '开门时间',
              `device_time` datetime NOT NULL COMMENT '上次开门时间',
              `result_code` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0为开门',
              `open_type` varchar(10) NOT NULL DEFAULT 'card' COMMENT 'card刷卡pin密码12远程',
              primary key (log_id)
            ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8
        ";
        $res = $this->mysql->exec($sql);
    }
}